/*--------------------------------------------------------------------------
 *  Copyright 2009 Taro L. Saito
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *--------------------------------------------------------------------------*/
//--------------------------------------
// XerialJ
//
// SilkDocument.java
// Since: 2009/12/07 19:21:31
//
// $URL$
// $Author$
//--------------------------------------
package org.xerial.silk.model;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.xerial.silk.parser.SilkElementHandler;
import org.xerial.silk.parser.SilkParser;

/**
 * Document object-model for Silk format
 * 
 * @author leo
 * 
 */
public class SilkDocument
{
    public List<SilkElement> line = new ArrayList<SilkElement>();

    public static SilkDocument parse(URL silkResource) throws Exception {
        SilkParser parser = new SilkParser(silkResource);
        SilkDocumentGenerator g = new SilkDocumentGenerator();

        parser.parse(g);

        return g.doc;
    }

    private static class SilkDocumentGenerator implements SilkElementHandler
    {
        SilkDocument doc = new SilkDocument();

        public void handle(SilkElement event) throws Exception {
            doc.line.add(event);
        }
    }

    //    public String toSilk() {
    //        StringBuilder buf = new StringBuilder();
    //        int lineCount = 0;
    //        for (SilkEvent e : line) {
    //            if (e.getType() == SilkEventType.END_OF_FILE)
    //                break;
    //
    //            if (lineCount++ > 0)
    //                buf.append(StringUtil.NEW_LINE);
    //            switch (e.getType()) {
    //            case BLANK_LINE:
    //                break;
    //            case COMMENT_LINE: {
    //                SilkCommentLine cl = SilkCommentLine.class.cast(e.getElement());
    //                buf.append(cl.line);
    //                break;
    //            }
    //            case DATA_LINE: {
    //                SilkDataLine dl = SilkDataLine.class.cast(e.getElement());
    //                buf.append(dl.getDataLine());
    //                break;
    //            }
    //            case FUNCTION: {
    //                SilkFunction sf = SilkFunction.class.cast(e.getElement());
    //                break;
    //            }
    //            case NODE: {
    //                SilkNode sn = SilkNode.class.cast(e.getElement());
    //                buf.append(sn.toSilk());
    //                break;
    //            }
    //            case PREAMBLE: {
    //                SilkPreamble sp = SilkPreamble.class.cast(e.getElement());
    //                buf.append(sp.toSilk());
    //                break;
    //            }
    //            }
    //
    //        }
    //
    //        return buf.toString();
    //    }

}
